﻿ /*
*	Flex 3.0 ActionScript code to drive AGSMAP viewer
*  						By Jie Gao, Oct. 2008
*
*   Dependent open source toolkits:
* 	1) corelib for json - by Open Source ActionScript 3 libraries
* 	2) Tree Checkbox - by sephiroth.it
*
*
*/
import mx.events.TreeEvent;
import mx.controls.Tree;
import mx.controls.treeClasses.ITreeDataDescriptor;
import mx.controls.Alert;
import mx.collections. *;
import com.ags.AgsLayer;
import mx.rpc.events.ResultEvent;
import com.adobe.serialization.json.JSON;
import mx.utils.ObjectUtil;
import com.esri.ags.toolbars.Navigation;
import mx.events.ItemClickEvent;
import mx.logging.LogEventLevel;
import com.esri.ags.geometry. *;
import flash.utils.Dictionary;
import com.esri.ags.layers.LayerInfo;
import com.esri.maplib.TOC;
import com.esri.maplib.tocClasses.TocLayerInfoItem;
import com.esri.maplib.tocClasses.TocItem;
import com.esri.maplib.tocClasses.TocMapLayerItem;

import com.benstucki.utilities.IconUtility;

/**
* Map toolbar handler
*
* @usage
* @param   event
* @return
*/

[Bindable]
private var serviceArray : Array = new Array ();


//private var servicesOnServer : ArrayCollection;
private var serviceFolders:Array=new Array();

/**
* Map Service Folder Parser
*
* @usage
* @param   event
* @return
*/
private function parseFolders (event : ResultEvent) : void
{
	var rawData : String = "[" + String (event.result) + "]";
	var arr : Array = (JSON.decode (rawData) as Array);
	for (var k : int = 0; k < arr [0].folders.length; k ++)
	{
		/*
		agsService.url = servicesPath + arr [0].folders [k] + "?f=json";
		agsService.send ();
		*/
		
		if (arr[0].folders[k]=='TMO'){
			agsService.url = servicesPath + arr [0].folders [k] + "?f=json";
			agsService.send ();
		}
		
	}
	//servicesOnServer = new ArrayCollection (serviceArray);
	//var dp:ArrayCollection = new ArrayCollection(serviceArray);
	//mapServiceCatalog.dataProvider = dp;
}
/**
* Map Services Parser
*
* @usage
* @param   event
* @return
*/
private function parseServices (event : ResultEvent) : void
{
	var mapServices : Array = new Array ();
	var rawData : String = "[" + String (event.result) + "]";
	var arr : Array = (JSON.decode (rawData) as Array);
	for (var i : int = 0; i < arr [0].services.length; i ++)
	{
		if (arr [0].services [i].type == "MapServer")
		mapServices.push (arr [0].services [i].name);
	}
	
	if (mapServices.length != 0)
	{
		for (var j : int = 0; j < mapServices.length; j ++)
		{
			var s : String = mapServices [j];
			var aa : Array = s.split ("/");
			if (aa [0] == "TMO")
			serviceArray.push (s);
		}
	}
	

		 
	//mapServiceCatalog.selectedIndex = 0;
	overlayMapLoader.url = "MapServiceQueryServlet";
	overlayMapLoader.send ();
	
}

/*
 
public function walkTree (tree : Tree, item : Object, startAtParent : Boolean = false) : void
{
	// get the Tree's data descriptor
	var descriptor : ITreeDataDescriptor = tree.dataDescriptor;
	var cursor : IViewCursor;
	var parentItem : Object;
	var childItem : Object;
	var childItems : Object;
	// if the item is null, stop
	if (item == null)
	return;
	// do we back up one level to the item's parent
	if (startAtParent)
	{
		// get the parent
		parentItem = tree.getParentItem (item);
		// is the parent real
		if (parentItem)
		{
			trace ("|-- Parent Node ", parentItem [tree.labelField]);
			// if the parent is a branch
			if (descriptor.isBranch (parentItem))
			{
				// if the branch has children to run through
				if (descriptor.hasChildren (parentItem))
				{
					// get the children of the branch
					// this part of the algorithm contains the item
					// passed
					childItems = descriptor.getChildren (parentItem);
				}
			}
			// if the branch has valid child items
			if (childItems)
			{
				// create our back step cursor
				cursor = childItems.createCursor ();
				// loop through the items parent's children (item)
				while ( ! cursor.afterLast)
				{
					// get the current child item
					childItem = cursor.current;
					var label : String = childItem [tree.labelField];
					var branch : Boolean = descriptor.isBranch (childItem);
					// good place for a custom method()
					//trace("Sibling Nodes :: ", label, "Is Branch :: ", branch);
					Alert.show ("sibling Node ");
					// if the child item is a branch
					if (descriptor.isBranch (childItem))
					// traverse the childs branch all the way down
					// before returning
					walkTree (tree, childItem);
					// do it again!
					cursor.moveNext ();
				}
			}
		}
	} 
	else // we don't want the parent OR this is the second iteration
	
	{
		// if we are a branch
		if (descriptor.isBranch (item))
		{
			// if the branch has children to run through
			if (descriptor.hasChildren (item))
			{
				// get the children of the branch
				childItems = descriptor.getChildren (item);
			}
			// if the child items exist
			if (childItems)
			{
				//Alert.show ('child nodes found ')
				// create our cursor pointer
				cursor = childItems.createCursor ();
				// loop through all of the children
				// if one of these children are a branch we will recurse
				while ( ! cursor.afterLast)
				{
					// get the current child item
					childItem = cursor.current;
					var label : String = childItem [tree.labelField];
					var branch : Boolean = descriptor.isBranch (childItem);
					// good place for a custom method()
					//if (childItem is TocMapLayerItem){
						//tree.setItemIcon(childItem, IconUtility.getClass(null, 'http://omhq531a:8399/arcgis/server/arcgisoutput/_ags_leg83d0fc632476453c927a6ed6e2d31ee6.png', 32, 32),IconUtility.getClass(tree, 'http://omhq531a:8399/arcgis/server/arcgisoutput/_ags_leg83d0fc632476453c927a6ed6e2d31ee6.png', 32, 32));
						//Alert.show (label);
					//}
					// if the child item is a branch
					if (descriptor.isBranch (childItem))
					// traverse the childs branch all the way down
					// before returning
					walkTree (tree, childItem);
					// check the next child
					cursor.moveNext ();
				}
			} else 
			{
				 
			}
		}
	}
}
*/

public function getDistanceInMile (pnt1 : MapPoint, pnt2 : MapPoint) : Number 
{
	var lon1 : Number = pnt1.x;
	var lat1 : Number = pnt1.y;
	var lon2 : Number = pnt2.x;
	var lat2 : Number = pnt2.y;
	var theta : Number = lon1 - lon2;
	var dist : Number = 0;
	try 
	{
		dist = Math.sin (deg2rad (lat1)) * Math.sin (deg2rad (lat2)) + Math.cos (deg2rad (lat1)) * Math.cos (deg2rad (lat2)) * Math.cos (deg2rad (theta));
		dist = Math.acos (dist);
		dist = rad2deg (dist);
		dist = dist * 60 * 1.1515;
	} catch (e : Error)
	{
		//
		
	}
	return (dist);
}
private function deg2rad (deg : Number) : Number 
{
	return (deg * Math.PI / 180.0);
}
private function rad2deg (rad : Number) : Number 
{
	return (rad * 180.0 / Math.PI);
}
public function getDistanceInFeet (pnt1 : MapPoint, pnt2 : MapPoint) : Number 
{
	var lon1 : Number = pnt1.x;
	var lat1 : Number = pnt1.y;
	var lon2 : Number = pnt2.x;
	var lat2 : Number = pnt2.y;
	var theta : Number = lon1 - lon2;
	var dist : Number = 0;
	try 
	{
		dist = Math.sin (deg2rad (lat1)) * Math.sin (deg2rad (lat2)) + Math.cos (deg2rad (lat1)) * Math.cos (deg2rad (lat2)) * Math.cos (deg2rad (theta));
		// avoid 'NaN'
		dist = Math.acos (dist);
		dist = rad2deg (dist);
		dist = dist * 60 * 1.1515;
		dist = mile2Feet (dist);
	} catch (e : Error)
	{
	}
	return (dist);
}
public function getMilesByDecimalDegree (degree : Number) : Number
{
	//return 6378137.0 * Math.PI* degree/ 180.0/1000;
	return 6371.0 * Math.PI * degree / 180.0 * 0.621371192;
}
public function getDecimalDegreeByMiles (m : Number) : Number 
{
	//return 180.0*m*1000/Math.PI/6378137.0;
	return 180.0 * m / Math.PI / 6371.0 / 0.621371192;
}
public function km2Mile (km : Number) : Number
{
	return km / 1.609344;
}
public function mile2Km (miles : Number) : Number
{
	return miles * 1.609344;
}
public function mile2Feet (m : Number) : Number
{
	return m * 5280;
}
public function feet2Mile (f : Number) : Number
{
	return f / 5280;
}
/**
* Calculate the length in mile of geographic line feature shape
* in decimal degree
*
* @param line
* @return
*/
/*
public Number getLengthInMile(line:Polyline){

Number len=0.0;
Point p=null;
for (int i=0; i<line.getNumPoints(); i++){
if (i==0){
p=line.getPointN(i);
continue;
}else{
len=len+getDistanceInMile(p.getY(), p.getX(), line.getPointN(i).getY(), line.getPointN(i).getX());
p=line.getPointN(i);
}
}
return len;
}
*/
